package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Loglar;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("LoglarBean")
@Scope("session")
public class LoglarBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Loglar selectedLog;	
	private ArrayList<Loglar> logList = new ArrayList<Loglar>();
	
	private MusteriAygitlariBean musAgtBen;
	private String LogTrhBitis;
	
	@Override
	@PostConstruct
	public void init(){		
		//logList = (List<Loglar>) emmyService.findAll(new Loglar());
	}	
	

	@Override
	public String restart(){
		init();
		return "";
	}
	
	@Override
	public String deleteView(){
		emmyService.delete(selectedLog);
		init();
		return "";
	}

	@Override
	public String viewNew() {
		selectedLog= new Loglar();
		return "";
	}

	@Override
	public String viewDetail() {
		if(selectedLog ==null){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Hata: ", "Seçim yapılmadı.");  
	        FacesContext.getCurrentInstance().addMessage(null, msg);
	        return "";
		}
		System.out.println("selected : "+selectedLog.getCozum());
		/*
		List<Loglar> valueList = (List<Loglar>) logTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = logTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentLog= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentLog.getLogid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		emmyService.saveOrUpdate(selectedLog);
		init();
		return "";
	}

	public void getAygitLogs(){
		logList.clear();
		try{
			musAgtBen = (MusteriAygitlariBean) emmyService.getBean(new MusteriAygitlariBean());
			for(Iterator<?> iter = musAgtBen.getSelectedMusAygit().getLoglars().iterator(); iter.hasNext();) {		        		
				Loglar log = (Loglar)iter.next();
				System.out.println("ip : "+log.getMusteriaygitlari().getAygitSeriNo());
				System.out.println("aygit id : "+log.getCozum() );
				logList.add(log);
				System.out.println("log size :"+logList.size());
			}
		}catch(java.lang.NullPointerException npe){
			System.out.println("LoglarBean : getAygitLogs NPE : "+npe.getMessage());
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", 
					"Seçim yapılmadı veya login hatası, Lütfen tekrar deneyiniz."); 
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}catch(Exception e){
			System.out.println("LoglarBean getAygitLogs err : "+e);
		}
	}
	@SuppressWarnings("unchecked")
	public String getDetayliLog(){		
		logList = (ArrayList<Loglar>) emmyService.getQuery(createQuery());
		return "";
	}
	
	@Override
	public String createQuery() {
		Loglar log  = selectedLog;		
		String query = "from Loglar";				
		
		if(log == null){
			return query;
		}
		if(log.getMusteriaygitlari().getAygitSeriNo() != 0){
			if(query.indexOf("where") != -1){
				query += " and aygitSeriNo="+log.getMusteriaygitlari().getAygitSeriNo()+"";
				
			}
			else{
				query += " where aygitSeriNo="+log.getMusteriaygitlari().getAygitSeriNo()+"";			
			}
		}
		if(log.getIp() != null){
			if(query.indexOf("where") != -1){
				query += " and ip="+log.getIp()+"";
				
			}
			else{
				query += " where ip="+log.getIp()+"";			
			}
		}
		if(log.getYapilanislem() != null){
			if(query.indexOf("where") != -1){
				query += " and yapilanislem like '%"+log.getYapilanislem()+"%'";
				
			}
			else{
				query += " where yapilanislem like '%"+log.getYapilanislem()+"%'";			
			}
		}
		if(log.getHata() != null){
			if(query.indexOf("where") != -1){
				query += " and hata hata '%"+log.getHata()+"%'";
				
			}
			else{
				query += " where hata hata '%"+log.getHata()+"%'";			
			}
		}
		if(log.getCozum() != null){
			if(query.indexOf("where") != -1){
				query += " and hata cozum '%"+log.getCozum()+"%'";
				
			}
			else{
				query += " where hata cozum '%"+log.getCozum()+"%'";			
			}
		}
		if(log.getTarih() != null){
			if(query.indexOf("where") != -1){
				query += " and (tarih between "+log.getTarih()+" and "+LogTrhBitis+")";
				
			}
			else{
				query += " where (tarih between "+log.getTarih()+" and "+LogTrhBitis+")";			
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	
	public ArrayList<Loglar> getLogList() {
		return logList;
	}

	public void setLogList(ArrayList<Loglar> logList) {
		this.logList = logList;
	}

	public Loglar getSelectedLog() {
		return selectedLog;
	}

	public void setSelectedLog(Loglar selectedLog) {
		this.selectedLog = selectedLog;
	}

	public String getLogTrhBitis() {
		return LogTrhBitis;
	}

	public void setLogTrhBitis(String logTrhBitis) {
		LogTrhBitis = logTrhBitis;
	}

	public MusteriAygitlariBean getMusAgtBen() {
		return musAgtBen;
	}

	public void setMusAgtBen(MusteriAygitlariBean musAgtBen) {
		this.musAgtBen = musAgtBen;
	}
}